import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("989811");
        Connection connection = dataSource.getConnection();
        Scanner sc = new Scanner(System.in);

        System.out.println("输入编号");
        int id = sc.nextInt();
        sc.nextLine();//吸收空格

        System.out.println("输入序号");
        String sn = sc.nextLine();

        System.out.println("输入姓名");
        String name = sc.nextLine();

        System.out.println("输入班级");
        int classes_id = sc.nextInt();

        String sql = "insert into student values(?, ?, ?, null, ?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, sn);
        statement.setString(3, name);
        statement.setInt(4, classes_id);
        statement.executeUpdate();

        statement.close();
        connection.close();
    }
}
